; eightplay.asm
; Joystick routine for the 8-player adapter
;

	.include "global.inc"
	_IN_EIGHTPLAY = 1
	.include "eightplay.inc"	
	
	.segment "ZEROPAGE"

jr0:		.res 1
jr1:		.res 1

	.segment "DATA"

joypos:	
joy0:		.byte $1F
joy1:		.byte $1F
joy2:		.byte $1F,$1F,$1F, $1F,$1F,$1F,$1F

	.segment "RODATA"
	
mux8:		.byte $00, $20, $40, $60, $80, $A0, $C0, $E0

	.segment "CODE"

;
;	Inits joysticks 1..8 (blinkenlights)
;	
jinit8:		lda #$E0
			sta VIA1_BASE+2
			rts

;
;	Inits joysticks 1..2 (protovision)
;	
jinit4:		lda #$80
			sta VIA1_BASE+2
			rts

;
;	Reads joysticks 1..8 (blinkenlights)
;	
jread8:		ldx #7
@readloop:	lda mux8, x
			sta VIA1_BASE+0
@doread:	lda VIA1_BASE+0
			cmp VIA1_BASE+0
			bne @doread
			and #$1F
			sta joy1, x
			dex
			bpl @readloop
			rts

;
;	Reads joystick port 1 & 2 (protovision adapter)
;
jread4:		lda #$80
			sta VIA1_BASE+0
@read1:		lda VIA1_BASE+0
			cmp VIA1_BASE+0
			bne @read1
			and #$1F
			sta joy1
			lda #$00
			sta VIA1_BASE+0
@read2:		lda VIA1_BASE+0
			cmp VIA1_BASE+0
			bne @read2
			tax
			and #$0F
			sta joy2
			txa
			and #$20
			lsr
			ora joy2
			sta joy2
			rts

;
;	Reads joystick port 0 (internal)
;
jread0:		lda VIA1_BASE+15
			asl
			asl
			sta jr0
			; right?
			ldx VIA2_BASE+2
			lda #%01111111
			sta VIA2_BASE+2
			lda VIA2_BASE+0
			stx VIA2_BASE+2
			sta jr1
			lda #0
			asl jr0
			rol
			asl jr1
			rol
			asl jr0
			rol
			asl jr0
			rol
			asl jr0
			rol
			sta joy0						
			rts

			